package service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.Setting;

import org.springframework.stereotype.Service;

@Service
public class SettingService extends BaseManager{
	
	
	public static Map<String,String> settingMap = new HashMap<String, String>();
	
	@SuppressWarnings("unchecked")
	public Map<String,String> reLoad(){
		List<Setting> settings = getDao().findAll("from Setting");
		settingMap = new HashMap<String, String>();
		for(Setting s : settings){
			settingMap.put(s.getCode(), s.getValue());
		}
		return settingMap;
	}
	
	public Setting getSetting(String code){
		return getDao().getByProperty(Setting.class, "code", code);
	}
	
	public Integer getIntValue(String code){
		String value = settingMap.get(code);
		return value ==null?null:Integer.valueOf(value);
	}
	
	public Double getDoubleValue(String code){
		String value = settingMap.get(code);
		return value ==null?null:Double.valueOf(value);
	}
	
	public BigDecimal getBigDecimalValue(String code){
		String value = settingMap.get(code);
		return value ==null?null:new BigDecimal(value);
	}
	
	public String getSettingValue(String code){
		return settingMap.get(code);
	}
	
	public void updateSettingMap(String code,String value){
		settingMap.put(code, value);
	}
}
